package gsons;

import java.lang.reflect.Type;

import models.Field;
import utils.Dicts;
import utils.Dicts.Bool;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * @author ultrafrog
 * @version 1.0, 2015-04-26
 * @since 1.0
 */
public class FieldJsonSerializer implements JsonSerializer<Field> {

	/* (non-Javadoc)
	 * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type, com.google.gson.JsonSerializationContext)
	 */
	@Override
	public JsonElement serialize(Field field, Type type, JsonSerializationContext context) {
		JsonObject obj = new JsonObject();
		
		obj.addProperty("id", field.id);
		obj.addProperty("name", field.name);
		obj.addProperty("code", field.code);
		obj.addProperty("type", field.type);
		obj.addProperty("length", field.toLengthString());
		obj.addProperty("nullable", Dicts.name(field.nullable, Bool.class));
		obj.addProperty("remark", field.toRemarkString());
		obj.add("dicts", context.serialize(field.dicts));
		
		return obj;
	}
}
